package com.holly.unit.deform;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.holly.unit.db.api.factory.PageFactory;
import com.holly.unit.db.api.factory.PageResultFactory;
import com.holly.unit.db.api.pojo.page.PageResult;
import com.holly.unit.deform.api.DeformRouterApi;
import com.holly.unit.deform.api.exception.DeformException;
import com.holly.unit.deform.api.exception.enums.DeformExceptionEnum;
import com.holly.unit.deform.api.pojo.route.DesignFormRouteDTO;
import com.holly.unit.deform.api.pojo.route.DesignFormRouteRequest;
import com.holly.unit.deform.context.DeformContext;
import com.holly.unit.deform.entity.DesignFormRoute;
import com.holly.unit.deform.service.DeformRouteService;
import com.holly.unit.office.api.OfficeExcelApi;
import com.holly.unit.office.api.pojo.report.ExcelExportParam;
import com.holly.unit.query.api.QueryApi;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 类描述: 表单设计器路由操作类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/7 11:04
 */
public class DeformRouteOperator implements DeformRouterApi<DesignFormRoute>{

    private DeformRouteService routeService = DeformContext.deformRouteService();

    private OfficeExcelApi officeExcelApi = DeformContext.officeExcelApi();

    private QueryApi queryApi = DeformContext.queryApi();

    @Override
    public void add(DesignFormRouteRequest designFormRouteRequest) {
        DesignFormRoute designFormRoute = new DesignFormRoute();
        BeanUtil.copyProperties(designFormRouteRequest, designFormRoute);
        routeService.save(designFormRoute);
    }

    @Override
    public void del(DesignFormRouteRequest designFormRouteRequest) {
        routeService.removeById(designFormRouteRequest.getId());
    }

    @Override
    public void edit(DesignFormRouteRequest designFormRouteRequest) {
        DesignFormRoute designFormRoute = new DesignFormRoute();
        BeanUtil.copyProperties(designFormRouteRequest, designFormRoute);
        routeService.updateById(designFormRoute);
    }

    @Override
    public DesignFormRouteDTO detail(DesignFormRouteRequest designFormRouteRequest) {
        DesignFormRouteDTO dto = new DesignFormRouteDTO();
        DesignFormRoute designFormRoute = routeService.getById(designFormRouteRequest.getId());
        BeanUtil.copyProperties(designFormRoute,dto);
        return null;
    }

    @Override
    public List<DesignFormRouteDTO> findList(DesignFormRouteRequest designFormRouteRequest) {
        List<DesignFormRoute> list = routeService.list(this.createWrapper(designFormRouteRequest));
        List<DesignFormRouteDTO> dtos = new ArrayList<>();
        list.stream().forEach(entity->{
            DesignFormRouteDTO dto = new DesignFormRouteDTO();
            BeanUtil.copyProperties(entity,dto);
            dtos.add(dto);
        });
        return dtos;
    }

    @Override
    public PageResult<DesignFormRouteDTO> findPage(DesignFormRouteRequest designFormRouteRequest) {
        Page<DesignFormRoute> page = routeService.page(PageFactory.defaultPage(), this.createWrapper(designFormRouteRequest));
        PageResult<DesignFormRoute> pageResult = PageResultFactory.createPageResult(page);
        List<DesignFormRouteDTO> dtos = new ArrayList<>();
        pageResult.getRows().stream().forEach(entity->{
            DesignFormRouteDTO dto = new DesignFormRouteDTO();
            BeanUtil.copyProperties(entity,dto);
            dtos.add(dto);
        });
        return createDtoPageResult(pageResult, dtos);
    }

    @Override
    public void export(HttpServletResponse response) {
        ExcelExportParam excelExportParam = new ExcelExportParam();
        List<DesignFormRoute> designFormRouteList = routeService.list();

        excelExportParam.setClazz(DesignFormRoute.class);
        excelExportParam.setDataList(designFormRouteList);
        excelExportParam.setExcelTypeEnum(ExcelTypeEnum.XLS);
        excelExportParam.setFileName("系统用户导出");
        excelExportParam.setResponse(response);

        officeExcelApi.easyExportDownload(excelExportParam);
    }

    @Override
    public PageResult<DesignFormRoute> fetchPageList(DesignFormRoute designFormRoute, Map<String, String[]> paramMap) {
        QueryWrapper<DesignFormRoute> queryWrapper = queryApi.initQueryWrapper(designFormRoute, paramMap);
        Page<DesignFormRoute> page = routeService.page(PageFactory.defaultPage(), queryWrapper);
        PageResult<DesignFormRoute> pageResult = PageResultFactory.createPageResult(page);
        return pageResult;
    }

    @Override
    public void delBatch(List<String> ids) {
        if (ObjectUtil.isNull(ids) || ids.size() == 0) {
            throw new DeformException(DeformExceptionEnum.DEFORM_DEL_PARAM_ERROR);
        }
        routeService.removeByIds(ids);
    }

    @Override
    public void delBatch(String ids) {
        if (ids == null || "".equals(ids.trim())) {
            throw new DeformException(DeformExceptionEnum.DEFORM_DEL_PARAM_ERROR);
        } else {
            delBatch(Arrays.asList(ids.split(",")));
        }
    }

    @Override
    public boolean editEntity(DesignFormRoute designFormRoute) {
        DesignFormRoute check = routeService.getById(designFormRoute.getId());
        if (ObjectUtil.isNull(check)) throw new DeformException(DeformExceptionEnum.DEFORM_QUERY_BY_ID_ERROR,check);
        return routeService.updateById(designFormRoute);
    }


    @Override
    public DesignFormRoute fetchEntity(LambdaQueryWrapper<DesignFormRoute> queryWrapper) {
        DesignFormRoute one = routeService.getOne(queryWrapper);
        if (ObjectUtil.isNull(one)) {
            throw new DeformException(DeformExceptionEnum.DEFORM_QUERY_BY_ID_ERROR,one);
        }
        return one;
    }

    @Override
    public DesignFormRoute fetchEntityById(Serializable id) {
        DesignFormRoute one = routeService.getById(id);
        if (ObjectUtil.isNull(one)) {
            throw new DeformException(DeformExceptionEnum.DEFORM_QUERY_BY_ID_ERROR,one);
        }
        return one;
    }

    private PageResult<DesignFormRouteDTO> createDtoPageResult(PageResult<DesignFormRoute> pageResult, List<DesignFormRouteDTO> dtos) {
        PageResult<DesignFormRouteDTO> res = new PageResult<>();
        res.setRows(dtos);
        res.setTotalRows(Convert.toInt(pageResult.getTotalRows()));
        res.setPageNo(pageResult.getPageNo());
        res.setPageSize(pageResult.getPageSize());
        res.setTotalPage(PageUtil.totalPage(pageResult.getTotalRows(), pageResult.getPageSize()));
        return res;
    }

    /**
     * 构建wrapper
     *
     * @author holly
     * @date 2021/1/13 10:50
     */
    private LambdaQueryWrapper<DesignFormRoute> createWrapper(DesignFormRouteRequest designFormRouteRequest) {
        LambdaQueryWrapper<DesignFormRoute> queryWrapper = new LambdaQueryWrapper<>();

        // SQL拼接
        //queryWrapper.eq(ObjectUtil.isNotNull(dictRequest.getDictId()), SysDict::getDictId, dictRequest.getDictId());
        queryWrapper.eq(ObjectUtil.isNotNull(designFormRouteRequest.getUpdateUser()), DesignFormRoute::getUpdateUser, designFormRouteRequest.getUpdateUser());
        queryWrapper.eq(ObjectUtil.isNotNull(designFormRouteRequest.getCreateUser()), DesignFormRoute::getCreateUser, designFormRouteRequest.getCreateUser());
        queryWrapper.eq(ObjectUtil.isNotNull(designFormRouteRequest.getStatus()), DesignFormRoute::getStatus, designFormRouteRequest.getStatus());
        queryWrapper.eq(ObjectUtil.isNotNull(designFormRouteRequest.getRoutePath()), DesignFormRoute::getRoutePath, designFormRouteRequest.getRoutePath());
        queryWrapper.eq(ObjectUtil.isNotNull(designFormRouteRequest.getRouteType()), DesignFormRoute::getRouteType, designFormRouteRequest.getRouteType());
        queryWrapper.eq(ObjectUtil.isNotNull(designFormRouteRequest.getRouteName()), DesignFormRoute::getRouteName, designFormRouteRequest.getRouteName());
        queryWrapper.eq(ObjectUtil.isNotNull(designFormRouteRequest.getDesformCode()), DesignFormRoute::getDesformCode, designFormRouteRequest.getDesformCode());
        queryWrapper.eq(ObjectUtil.isNotNull(designFormRouteRequest.getDesformId()), DesignFormRoute::getDesformId, designFormRouteRequest.getDesformId());
        queryWrapper.eq(ObjectUtil.isNotNull(designFormRouteRequest.getId()), DesignFormRoute::getId, designFormRouteRequest.getId());
        return queryWrapper;
    }
}
